﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;

namespace Business.Cache
{
    class CacheProviderImpl : ICacheProvider
    {
        private ObjectCache Cache { 
            get 
            { 
                return MemoryCache.Default; 
            } 
        }

        public object Get(string key)
        {
            return Cache[key];
        }

        public void Set(string key, object data, int cacheTime)
        {
            if (Get(key) == null)
            {
                lock (new Object())
                {
                    if (Get(key) == null)
                    {
                        CacheItemPolicy policy = new CacheItemPolicy();
                        policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);

                        Cache.Add(new CacheItem(key, data), policy);
                    }
                }
            }
        }

        public bool IsSet(string key)
        {
            return (Cache[key] != null);
        }

        public void Invalidate(string key)
        {
            Cache.Remove(key);
        }
    }

}
